<template>
  <q-dialog class="my-dialog-form" v-model="isFlagShow">
    <q-card class="my-card q-mx-lg">
      <q-toolbar class="row items-center my-form-header">
        <div>
          {{dlgTitle}}
        </div>
        <q-space />
        <q-btn flat round dense icon="mdi-close" @click="close"/>
      </q-toolbar>
      <q-separator />
      <q-card-setion class="q-mx-sm">
        <q-scroll-area class="my-form-container">
          <table class="my-form-table">
            <tr class="my-row-height">
              <td class="my-label"><span style="color: red">**</span>报警ID：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.alarmId"
                  @blur="v$.alarmId.$touch()"
                  :error="v$.alarmId.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
              <td class="my-label">&nbsp;&nbsp;分配人：</td>
              <td class="my-value">
                <q-input
                  outlined
                  dense
                  v-model="param.dispatchUser"
                  @blur="v$.dispatchUser.$touch()"
                  :error="v$.dispatchUser.$error"
                  error-message="请输入不少于2个字符！"
                />
              </td>
            </tr>
            <tr class="my-row-height">
              <td class="my-label"><span style="color: red">**</span>分配处理：</td>
              <td class="my-value">
                <q-select
                  dense
                  outlined
                  clearable
                  options-dense
                  :options="userArr"
                  v-model="param.disposeUser"
                  option-value="label"
                  emit-value
                  options-selected-class="teal"
                  @blur="v$.disposeUser.$touch()"
                  :error="v$.disposeUser.$error"
                  error-message="请选择处理人！"
                />
              </td>
            </tr>
          </table>
        </q-scroll-area>
      </q-card-setion>
      <q-separator />
      <q-separator />
      <div class="q-gutter-sm my-form-footer">
        <q-btn
          unelevated
          dense
          label="提交"
          type="submit"
          color="teal-3"
          @click="onSubmit"
          style="width: 80px"
        />
        <q-btn
          unelevated
          dense
          label="重置"
          type="reset"
          color="orange-3"
          @click="onReset"
          style="width: 80px"
        />
      </div>
    </q-card>
  </q-dialog>
</template>

<script lang="ts">
import { defineAsyncComponent, defineComponent, onMounted, ref, toRefs, PropType, watch, toRef } from 'vue';
import { isObjectEmpty } from '../../../util/utils';
import { required } from '@vuelidate/validators'
import { useVuelidate } from '@vuelidate/core';
import { cataItem } from '../../../hook/common/useCatalogType';
import { useCatalogTool } from 'src/hook/common/useCatalogTool';
import { CinAlarmNodeVO, CinAlarmDisposeItemVO } from './AlarmGridModel';
import { useCommonStore } from 'stores/commonStore';
import { storeToRefs } from 'pinia';
import api from 'axios';
import { cinBaseUrlAPI } from 'src/util/CinBaseConst';
import { useMessageNotify } from 'src/hook/common/useMessageNotify';

const AmyImageUpload = defineAsyncComponent(() => import('components/basic/AmyImageUpload.vue'));

export default defineComponent({
  cname: 'AlarmDlg',
  components: {
    AmyImageUpload
  },
  props: {
    itemData: {
      type: Object as PropType<CinAlarmNodeVO>,
      required: true
    },
    disposeTypeArr: {
      type: Array as PropType<cataItem[]>,
      default: () => {
        return []
      }
    },
    userArr: {
      type: Array as PropType<cataItem[]>,
      default: () => {
        return []
      }
    }
  },
  emits: ['submit-alarm-item', 'cancel-alarm-item'],
  setup (props, context) {
    //数据解构
    const {
      disposeTypeArr,
      userArr,
      itemData
    } = toRefs(props);

    //*******************************
    //      store
    //*******************************
    const commonStore = useCommonStore();
    const {
      user
    } = storeToRefs(commonStore);

    //*******************************
    //      目录操作
    //*******************************
    const {
      findCatalogId
    } = useCatalogTool();

    //*******************************
    //      消息通知
    //*******************************
    //消息通知
    const {
      showMessage
    } = useMessageNotify();

    //*******************************
    //      表单项
    //*******************************
    //窗口显示
    let isFlagShow = ref(false);

    //窗口标题
    let dlgTitle = ref('');

    //界面数据
    let param = ref<CinAlarmDisposeItemVO>({
      id: 0,
      projectId: 0,
      alarmId: 0,
      disposeUser: '',
      disposeType: '未处理',
      description: '',
      dispatchUser: ''
    });

    //处理状态
    const disposeType = ref('');

    //关闭窗口
    const close = () => {
      isFlagShow.value = false;
      context.emit('cancel-alarm-item');
    }

    //显示窗口
    const show = (text: string) => {
      isFlagShow.value = true;
      dlgTitle.value = text;
    }

    //提交窗口
    const onSubmit = () => {
      v$.value.$validate();
      if (v$.value.$error) {
        alert('输入参数校验错误!');
      } else {
        isFlagShow.value = false;

        //发送数据
        api.post(cinBaseUrlAPI.CIN_ALARM_DISPOSAL_API_ADD, {
          id: 0,
          projectId: param.value.projectId,
          alarmId: param.value.alarmId,
          disposeUserId: findCatalogId(userArr.value, param.value.disposeUser),
          disposeTypeId: findCatalogId(disposeTypeArr.value, param.value.disposeType),
          dispatchUserId: findCatalogId(userArr.value, param.value.dispatchUser),
          description: param.value.description
        }).then((res) => {
          if (res.data.message === 'Success') {
            showMessage('分配成功！');
          } else {
            showMessage(res.data.message);
          }
        }).catch((error) => {
          showMessage(error);
        });
      }
    }

    //重置
    const onReset = () => {
      if (param.value) {
        param.value.projectId = itemData.value.projectId;
        param.value.disposeUser = user.value.username;
        param.value.disposeType = '';
        param.value.dispatchUser = user.value.username;
        param.value.description = '';
      }

      //表单验证初始化
      v$.value.$reset();
    }


    //*******************************
    //      表单校验
    //*******************************
    //配置校验规则
    const rules = {
      id: {},
      alarmId: {},
      disposeUser: { required },
      dispatchUser: {}
    }

    //使用vuelidate，建立变量对应的校验规则
    const v$ = useVuelidate(rules, {
      id: toRef(param.value, 'id'),
      alarmId: toRef(param.value, 'alarmId'),
      disposeUser: toRef(param.value, 'disposeUser'),
      dispatchUser: toRef(param.value, 'dispatchUser')
    });

    //*******************************
    //      启动模块
    //*******************************
    //初始化
    const init = () => {
      if (!isObjectEmpty(itemData.value)) {
        param.value.id = 0;
        param.value.projectId = itemData.value.projectId;
        param.value.disposeType = '未处理';
        param.value.disposeUser = user.value.username;
        param.value.dispatchUser = user.value.username;
        param.value.description = '';
      }

      //表单验证初始化
      v$.value.$reset();
    }

    //监听对象的所有属性
    watch(() => itemData, () => {
      init();
    }, { deep: true });

    //启动挂载组件
    onMounted(() => {
      init();
    })

    return {
      disposeTypeArr,
      userArr,
      itemData,

      isFlagShow,
      dlgTitle,
      param,
      v$,
      init,
      close,
      show,
      onSubmit,
      onReset
    }
  }
})
</script>

<style lang="scss" scoped>
.my-card {
  alignment: center;
  width: 800px;
  max-width: 800px;
  .my-form-container {
    text-align: center;
    padding: 5px 50px 5px 50px;
    height: 400px;
  }
}

</style>
